/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MainPack;

/**
 *
 * @author Van persie
 */
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Graphics;
import MaterialPack.*;
import EntityGameObject.*;
import Supporter.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.obex.HeaderSet;

/**
 *
 * @author Van persie
 */
public class PetManagement extends AbstractCanvas {
    //private Image bufferGroup;

    private Vector groups;
    SubGroup ShopGroup = null;
    SubGroup SkillGroup = null;
    private boolean isChanging = false;
    private int currentGrp;
    private int shrinkGrp;
    private int directChanging;
    private int xGroupPos;
    private int yGroupPos;
    private int widthGroup;
    private int heightGroup;
    private Player ss;
    // Control
    Button AgilityBtn = null;
    InfoPanel AgibilityVal = null;
    InfoPanel SpeedVal = null;
            
    //InfoPanel SpeedPnl = null;
    Button StrengthBtn = null;
    InfoPanel StrengthVal = null;
    InfoPanel StaminaVal = null;
    Button toPlayBtn = null;
    Button backToMenu = null;
    // animation
    private Timer timer;
    private Sticker sticker;
    // List Item-Shop
    PetItem[] listItemShop = null;
    int NumOfItems = 0;
    boolean[] CheckedItems = null;
    int tempBill = 0;
    Vector listShop = null;
    // Pet 
    PetInstance petInstance = null;
    //Image Rule = null;
    InfoPanel MoneyLabel = null;
    //InfoPanel MoneyVal = null;
    InfoPanel PointsLabel = null;
    //InfoPanel PointsVal = null;
    //InfoBar SpeedBar = null;
    //InfoBar StaminaBar = null;
    int scalar = 40;
    // buffer 
    Vector listBackObjs;
    Image BufferImg = null;
    //Graphics BufferGraphics = null;

    public PetManagement(MainMIDlet midlet, Player player) {

        main = midlet;
        ss = player;
        //petInstance = new PetInstance("/PetPhoto.png", 2, 2, 40, 40, main.getPet());
        height = this.getHeight();
        width = this.getWidth();


        CheckedItems = new boolean[10];
        for (int i = 0; i < 10; i++) {
            CheckedItems[i] = false;
        }

        listBackObjs = new Vector();
        LoadShopPrototype();

        try {
            if (width != height) {
                LoadResource();
            }
        } catch (Exception e) {
        }

        timer = new Timer();

        //StartTimer();
    }

    PetManagement(MainMIDlet aThis) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    
    public void LoadResource() throws IOException {



        ImageSupporter imgSupporter = new ImageSupporter();
        System.out.println("Begin Load pet");
        LoadPet();
        System.out.println("end Load pet");
        LoadBackground(imgSupporter);
        System.out.println("end Load ack");
        LoadManagementGroups(imgSupporter);
        System.out.println("Begin Load grp");

        LoadPetInfo();
        System.out.println("Begin Load pet info");
        paintBackground();
        System.out.println("Begin pain back");

    }

    public void LoadBackground(ImageSupporter imgSupporter) {
        // background & items in backgrounds
        // back buffer
        BufferImg = Image.createImage(width, height);
        //BufferGraphics = BufferImg.getGraphics();

        Image temp = imgSupporter.getImage("/option-background.jpg");
        background = ImageSupporter.scaleImage(temp, width, height);
        //Rule = ImageSupporter.scaleImage(imgSupporter.getImage("/Rule.png"), 40, 2*height/3 ); 
        temp = imgSupporter.getImage("/left-button-arrow.png");
        temp = Image.createImage(temp, 0, 0, temp.getWidth(), temp.getHeight(), Sprite.TRANS_ROT180);
        Image temp2 = imgSupporter.getImage("/left-button-arrow-press.png");
        temp2 = Image.createImage(temp2, 0, 0, temp.getWidth(), temp.getHeight(), Sprite.TRANS_ROT180);
        toPlayBtn = new Button(temp, temp2, null, 7 * width / 8 + 5, 9 * height / 10 - 20, width / 8, height / 10);

        temp = imgSupporter.getImage("/Return.png");
        backToMenu = new Button(Image.createImage(temp, 0, 0, temp.getWidth(), temp.getHeight(), Sprite.TRANS_MIRROR_ROT180), null, width / 16, 2, width / 8, height / 10);


        listBackObjs.addElement(toPlayBtn);
        listBackObjs.addElement(backToMenu);
    }

    public void LoadShopGRP(Image plane, Image Act) {
        //       SHOP
        int wid = width / 20;
        int hei = height / 20;
        ShopGroup = new SubGroup(plane, Act, "Shop", xGroupPos, yGroupPos, widthGroup, heightGroup);


        listShop = ShopGroup.getItems();
        if (!listShop.isEmpty()) {
            listShop.removeAllElements();
        }



        MoneyLabel = new InfoPanel(null, "Money : " + Integer.toString(petInstance.GetAttributes(Pet.MONEY)) + " $", xGroupPos + 2*wid, yGroupPos + hei, 2 * scalar, scalar, Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
        MoneyLabel.SetColorText(200, 200, 200);
        MoneyLabel.setNoBackground(true);
//        MoneyVal = new InfoPanel(null, Integer.toString(petInstance.GetAttributes(Pet.MONEY)) + " $", xGroupPos + wid + 5 * scalar / 3, yGroupPos + hei, scalar, 2 * scalar / 3, Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
//        MoneyVal.SetColorText(255, 255, 5);
//        MoneyVal.TransBackground(true);
        ShopGroup.Add(MoneyLabel);
        //ShopGroup.Add(MoneyVal);


        int dem = Math.abs(GlobalVariables.myRandom.nextInt() % 3) + 3;
        int rd = Math.abs(GlobalVariables.myRandom.nextInt() % 6);
        while (dem >= 0) {
            ShopGroup.Add(listItemShop[rd]);
            dem--;
            rd = (rd + 1) % NumOfItems;
        }

//                ShopGroup.Add(listItemShop[0]);
//                ShopGroup.Add(listItemShop[2]);
//                ShopGroup.Add(listItemShop[3]);
//                ShopGroup.Add(listItemShop[1]);

        ShopGroup.ArrangeItem();

        groups.addElement(ShopGroup);
    }

    public void LoadSkillGRP(Image plane, Image Act) {
        // bufferGroup = ImageSupporter.alphaBlend(ImageSupporter.scaleImage(imgSupporter.getImage("/Plane.jpg"), widthGroup, heightGroup));

        int wid = width / 20;
        int hei = height / 20;

        ImageSupporter imgSpp = new ImageSupporter();
        Image button = imgSpp.getImage("/PlusBtn1.jpg");
        // SKILL
        SkillGroup = new SubGroup(plane, Act, "Skill ", xGroupPos, yGroupPos, widthGroup, heightGroup);


        PointsLabel = new InfoPanel(null, "Points : " + Integer.toString(petInstance.GetAttributes(Pet.POINTS)), xGroupPos + 2 * wid, yGroupPos + 7 * hei / 3, 80, scalar * 2 / 3, Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE));

        PointsLabel.SetColorText(200, 200, 200);
        PointsLabel.setNoBackground(true);
        PointsLabel.setAnchorMode(InfoPanel.ANC_MIDDLE);
//        PointsVal = new InfoPanel(null, Integer.toString(petInstance.GetAttributes(Pet.POINTS)), PointsLabel.getxPos() + PointsLabel.getWidth() / 2 + 2 * wid, PointsLabel.getyPos() + 2, scalar, scalar * 2 / 3, Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
//        PointsVal.SetColorText(100, 100, 100);
//        PointsVal.SetAlphaBack();
//        PointsVal.TransBackground(true);
//        PointsVal.setAnchorMode(InfoPanel.ANC_MIDDLE);

        SkillGroup.Add(PointsLabel);
        //SkillGroup.Add(PointsVal);

        //InfoPanel temp = null;
         AgilityBtn = new Button(button, null, xGroupPos + wid*3, yGroupPos + 3*(hei + scalar)/2, scalar, scalar);
         
         AgibilityVal = new InfoPanel(null, " Agility : " + Integer.toString(petInstance.GetAgility()) , AgilityBtn.getxPos() + AgilityBtn.getWidth()/2 , AgilityBtn.getyPos() , 3*scalar/2, scalar*2/3,  Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_MEDIUM));
            AgibilityVal.SetColorText(255, 5, 5);
             AgibilityVal.setNoBackground(true);
             AgibilityVal.setAnchorMode(InfoPanel.ANC_LEFT);
         SpeedVal = new InfoPanel(null, " Speed : " + Integer.toString(petInstance.getSpeed()) , AgilityBtn.getxPos() + AgilityBtn.getWidth()/2 , AgilityBtn.getyPos()+ AgilityBtn.getHeight() - hei  , 3*scalar/2, scalar*2/3,  Font.getFont(Font.FACE_SYSTEM,Font.STYLE_ITALIC  ,Font.SIZE_SMALL));
             SpeedVal.SetColorText(200, 5, 5);
             SpeedVal.setNoBackground(true);
             SpeedVal.setAnchorMode(InfoPanel.ANC_LEFT);
        //SpeedBar = new InfoBar("/option-expbar.png","/option-expbar-mask.png", InfoBar.HORIZON, petInstance.GetAttributes(Pet.SPEED)*10,AgilityBtn.getxPos() + AgilityBtn.getWidth()/2 + wid ,AgilityBtn.getyPos() + AgilityBtn.getHeight() - hei , 80, 15);
        //SpeedBar.SetColor(255, 0, 0);
             
//         SpeedPnl = new InfoPanel(null,Integer.toString(petInstance.getSpeed()),temp.getxPos() + 13*wid/2,AgilityBtn.getyPos() , scalar, scalar*2/3, Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_MEDIUM));
//         SpeedPnl.SetColorText(255, 50, 50);
//         SpeedPnl.SetAlphaBack();
//         SpeedPnl.TransBackground(true);
         
         SkillGroup.Add(AgilityBtn);
         SkillGroup.Add(AgibilityVal);
         SkillGroup.Add(SpeedVal);
//         SkillGroup.Add(temp);           
//         SkillGroup.Add(SpeedPnl);
         
         
         StrengthBtn = new Button(button,null, xGroupPos + wid*3, yGroupPos + 5*(hei + scalar)/2, scalar, scalar);
         StrengthVal = new InfoPanel(null, " Strength : " + Integer.toString(petInstance.GetStrength()), StrengthBtn.getxPos() + StrengthBtn.getWidth()/2 , StrengthBtn.getyPos() , 2*scalar, scalar*2/3,  Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_MEDIUM));
            StrengthVal.SetColorText(5, 5, 255);
             StrengthVal.setNoBackground(true);
             StrengthVal.setAnchorMode(InfoPanel.ANC_LEFT);
          StaminaVal =    new InfoPanel(null, " Stamina : " + Integer.toString(petInstance.getStamina()), StrengthBtn.getxPos() + StrengthBtn.getWidth()/2 , StrengthBtn.getyPos() + AgilityBtn.getHeight() - hei  , 2*scalar, scalar*2/3,  Font.getFont(Font.FACE_SYSTEM,Font.STYLE_ITALIC  ,Font.SIZE_SMALL));
          StaminaVal.SetColorText(5, 5, 200);
             StaminaVal.setNoBackground(true);
             StaminaVal.setAnchorMode(InfoPanel.ANC_LEFT);

         
         
         SkillGroup.Add(StrengthBtn);
         SkillGroup.Add(StrengthVal);           
         SkillGroup.Add(StaminaVal);
         
        groups.addElement(SkillGroup);
    }

    public void LoadManagementGroups(ImageSupporter imgSupporter) {
        //group 

        xGroupPos = scalar;
        yGroupPos = scalar / 3;
        widthGroup = width - (2 * scalar / 3 + 2);
        heightGroup = height - (height / 5 + scalar / 4);


        if (groups != null) {
            groups.removeAllElements();
        } else {
            groups = new Vector();
        }

        Image plane = imgSupporter.getImage("/Plane.png");
        plane = ImageSupporter.transparent(ImageSupporter.alphaBlend(ImageSupporter.scaleImage(plane, widthGroup, heightGroup)));

        Image ActiveName = plane;

        // skill
        LoadSkillGRP(plane, ActiveName);
        // shop
        LoadShopGRP(plane, ActiveName);

        currentGrp = 0;
        ((SubGroup) groups.elementAt(currentGrp)).setIsVisible(true);
    }

    public void Reset() {
        LoadShopPrototype();
        if (!groups.isEmpty()) {
            groups.removeAllElements();
        }
        //LoadShopGRP();
        for (int i = 0; i < CheckedItems.length; i++) {
            CheckedItems[i] = false;
        }
        tempBill = 0;
        LoadManagementGroups(new ImageSupporter());
        ShowPetInfo();
    }

    public void LoadShopPrototype() {
        NumOfItems = 6;
        listItemShop = new PetItem[NumOfItems];
        //for(int i = 0; i < 5 ; i++)
        Image checker = new ImageSupporter().getImage("/button-tick.png");

        listItemShop[0] = new PetItem("/chocolate.png", checker, "Socola", xGroupPos + 10, yGroupPos + 10, scalar, scalar, PetItem.TYPE_CHECK);
        listItemShop[1] = new PetItem("/vitamin.png", checker, "Vitamin", xGroupPos + 10, yGroupPos + 10, scalar, scalar, PetItem.TYPE_CHECK);
        listItemShop[2] = new PetItem("/strawberry.png", checker, "Strawberry", xGroupPos + 10, yGroupPos + 10, scalar, scalar, PetItem.TYPE_CHECK);
        listItemShop[3] = new PetItem("/milk.png", checker, "Milk", xGroupPos + 10, yGroupPos + 10, scalar, scalar, PetItem.TYPE_CHECK);
        listItemShop[4] = new PetItem("/banana.png", checker, "Banana", xGroupPos + 10, yGroupPos + 10, scalar, scalar, PetItem.TYPE_CHECK);
        listItemShop[5] = new PetItem("/apple.png", checker, "Apple", xGroupPos + 10, yGroupPos + 10, scalar, scalar, PetItem.TYPE_CHECK);

    }

    public void LoadPet() {
        // Pet photo

        Pet pet = main.getPet();
        petInstance = new PetInstance(width /6 , height, width / 3, height / 4, main.getPet(), false);

        listBackObjs.addElement(petInstance);
    }

    public void LoadPetInfo() {

        int x = width / 3 + 10;
        int y = 30;
        // set up


        InfoPanel temp;

        temp = new InfoPanel(null, "Level " + Integer.toString(petInstance.GetAttributes(Pet.LEVEL)), petInstance.getxPos() + petInstance.getWidth() / 2, petInstance.getyPos() - 2 * petInstance.getHeight() / 3, scalar * 3, scalar * 2 / 3, Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE));
        temp.SetColorText(100, 150, 50);
        temp.setNoBackground(true);
        temp.setAnchorMode(InfoPanel.ANC_LEFT);
        listBackObjs.addElement(temp);


//        temp = new InfoPanel(null, "Speed ",petInstance.getxPos() + petInstance.getWidth()/2 ,petInstance.getyPos()- petInstance.getHeight() + scalar , 2*scalar, scalar*2/3, Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_MEDIUM));
//        temp.SetColorText(255, 50, 50);
//        temp.setNoBackground(true);
//        temp.setAnchorMode(InfoPanel.ANC_LEFT);
        //listBackObjs.addElement(temp);


//        SpeedBar = new InfoBar("/option-expbar.png","/option-expbar-mask.png", InfoBar.HORIZON, petInstance.GetAttributes(Pet.SPEED)*10,petInstance.getxPos() + petInstance.getWidth()/2 ,petInstance.getyPos()-petInstance.getHeight() + 9*scalar/8 + 15 , 127, 14);
//        SpeedBar.SetColor(255, 0, 0);
//        listBackObjs.addElement(SpeedBar);
    }

    public void ShowPetInfo() {
        PointsLabel.setText("Points : " + Integer.toString(petInstance.GetAttributes(Pet.POINTS)));
        AgibilityVal.setText("Agility : " + Integer.toString(petInstance.GetAgility()));
        SpeedVal.setText("Speed : " + Integer.toString(petInstance.getSpeed()));
        
        //SpeedVal.setRatio(petInstance.GetAttributes(Pet.SPEED)*10);
        StrengthVal.setText("Strength : " + Integer.toString(petInstance.GetStrength()));
        StaminaVal.setText("Stamina : " + Integer.toString(petInstance.getStamina()));
        //StaminaPnl.setText(Integer.toString(petInstance.GetAttributes(Pet.TONGUELEN)));
        MoneyLabel.setText("Money : " + Integer.toString(petInstance.GetAttributes(Pet.MONEY)-tempBill) + " $");
        paintBackground();
    }

    protected void keyReleased(int key) {
        int n = groups.size();
        switch (key) {
            case KEY_NUM4:
            case LEFT:
                if (isChanging) {
                    Skip();
                }
                shrinkGrp = currentGrp;
                currentGrp = (currentGrp - 1 + n) % n;
                Change(Graphics.LEFT);
                break;

            case KEY_NUM6:
            case RIGHT:
                if (isChanging) {
                    Skip();
                }
                shrinkGrp = currentGrp;
                currentGrp = (currentGrp + 1) % n;
                Change(Graphics.RIGHT);

                break;
            case KEY_NUM0:
                main.exitMIDlet();
                break;
        }
        repaint();
    }

    public void Skip() {
        ((SubGroup) groups.elementAt(shrinkGrp)).setIsVisible(false);
        ((SubGroup) groups.elementAt(currentGrp)).setIsVisible(true);
        isChanging = false;
    }

    public void Change(int direction) {

        ((SubGroup) groups.elementAt(shrinkGrp)).Back();
        ((SubGroup) groups.elementAt(currentGrp)).Front();
        directChanging = direction;
        StartTimer();
    }

    protected void sizeChanged(int w, int h) {

        width = w;
        height = h;
        if (listBackObjs != null) {
            listBackObjs.removeAllElements();
        }
        try {
            LoadResource();
        } catch (Exception e) {
            System.out.println("error in size changed ! ");
        }

    }

    protected void pointerDragged(int x, int y) {
    }

    protected void pointerPressed(int x, int y) {

        for (int i = 0; i < groups.size(); i++) {
            ((MaterialObject) groups.elementAt(i)).Press(x, y);
        }
        for (int j = 0; j < listBackObjs.size(); j++) {
            ((MaterialObject) listBackObjs.elementAt(j)).Press(x, y);
        }
        paintBackground();
        repaint();
    }

    protected void pointerReleased(int x, int y) {
//        for(int i=0; i <listObjects.size(); i++)
//            ((MaterialObject)listObjects.elementAt(i)).Release(x,y);
        if (toPlayBtn.Release(x, y)) {
            try {
                if (GlobalVariables.sound) {
                try {
                    ss.stop();
                    try {
                        ss.deallocate();
                        ss = Manager.createPlayer(getClass().getResourceAsStream("/buttonclick.wav"), "audio/X-wav");
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    } catch (MediaException ex) {
                        ex.printStackTrace();
                    }
                    ss.setLoopCount(1);
                    ss.start();
                } catch (MediaException ex) {
                    ex.printStackTrace();
                }
            }
                main.SwitchTo(5);
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (MediaException ex) {
                ex.printStackTrace();
            }

        } else if (backToMenu.Release(x, y)) {

            try {
                if (GlobalVariables.sound) {
                try {
                    ss.stop();
                    try {
                        ss.deallocate();
                        ss = Manager.createPlayer(getClass().getResourceAsStream("/buttonclick.wav"), "audio/X-wav");
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    } catch (MediaException ex) {
                        ex.printStackTrace();
                    }
                    ss.setLoopCount(1);
                    ss.start();
                } catch (MediaException ex) {
                    ex.printStackTrace();
                }
            }
                main.SwitchTo(MainMIDlet.SCENE_MENU);
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (MediaException ex) {
                ex.printStackTrace();
            }
        }

        if(AgilityBtn.Release(x, y) && petInstance.GetAttributes(Pet.POINTS) > 0 )
        {
            //petInstance.AddAttributes(Pet.SPEED, 1);
            petInstance.SetAgility(petInstance.GetAgility() + 1 );
            petInstance.setSpeed(petInstance.GetAgility()*3/2);
            petInstance.AddAttributes(Pet.POINTS, -1);
            ShowPetInfo();

        }
        
        if(StrengthBtn.Release(x, y) && petInstance.GetAttributes(Pet.POINTS) > 0)
        {
            petInstance.AddAttributes(Pet.TONGUELEN, 1);
            petInstance.AddAttributes(Pet.POINTS, -1);
            ShowPetInfo();
        }

        if (petInstance.Release(x, y)) {
            // draw something
            if (GlobalVariables.sound) {
                try {
                    ss.stop();
                    try {
                        ss.deallocate();
                        ss = Manager.createPlayer(getClass().getResourceAsStream("/launch.wav"), "audio/X-wav");
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    } catch (MediaException ex) {
                        ex.printStackTrace();
                    }
                    ss.setLoopCount(1);
                    ss.start();
                } catch (MediaException ex) {
                    ex.printStackTrace();
                }
            }
            for (int i = 0; i < CheckedItems.length; i++) {
                if (CheckedItems[i] == true) {
                    petInstance.AddItem((PetItem) listShop.elementAt(i));
                    CheckedItems[i] = false;
                }
            }

            paintBackground();
            try {
                // do something
                main.SwitchTo(MainMIDlet.SCENE_PLAYPET);
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (MediaException ex) {
                ex.printStackTrace();
            }

        }
        SubGroup grp = null;
        for (int i = 0; i < groups.size(); i++) {
            grp = ((SubGroup) groups.elementAt(i));
            if (grp.Release(x, y)) {
                if (isChanging) {
                    Skip();
                }
                shrinkGrp = currentGrp;
                currentGrp = (currentGrp + 1) % groups.size();
                Change(Graphics.RIGHT);
            }
            if (grp.getItems().size() > 0) {
                for (int j = 0; j < grp.getItems().size(); j++) {
                    if (((PetItem) grp.getItems().elementAt(j)).Release(x, y)) {
                        if (((PetItem) grp.getItems().elementAt(j)).isActived() == false) {
                            if (petInstance.GetAttributes(Pet.MONEY) >= (((PetItem) grp.getItems().elementAt(j)).getPrice() + tempBill)) {
                                //petInstance.BuyItem(((PetItem)grp.getItems().elementAt(j)));
                                CheckedItems[j] = true;
                                tempBill += ((PetItem) grp.getItems().elementAt(j)).getPrice();
                                ((PetItem) grp.getItems().elementAt(j)).setActived(true);
                            }
                        } else {
                            //petInstance.ReturnItem(((PetItem)grp.getItems().elementAt(j)));
                            CheckedItems[j] = false;
                            tempBill -= ((PetItem) grp.getItems().elementAt(j)).getPrice();
                            ((PetItem) grp.getItems().elementAt(j)).setActived(false);
                        }
                        
                    }
                }
                ShowPetInfo();
            }
        }
        repaint();
    }

    public void commandAction(Command command, Displayable displayable) {
    }

    public void TimerStick() {
        boolean bool1 = ((SubGroup) groups.elementAt(currentGrp)).Expand(directChanging);
        boolean bool2 = ((SubGroup) groups.elementAt(shrinkGrp)).Shrink(directChanging);
        if (bool1 == true || bool2 == true) {
            PauseTimer();
            System.out.println("Finish ");

        }
        repaint();
    }

    public void paint(Graphics g) {

        g.drawImage(BufferImg, 0, 0, Graphics.TOP | Graphics.LEFT);
//        for(int i=0; i <listObjects.size(); i++)
//            ((MaterialObject)listObjects.elementAt(i)).paint(g);
        for (int i = 0; i < groups.size(); i++) {
            ((MaterialObject) groups.elementAt(i)).paint(g);
        }
        //toPlayBtn.paint(g);
    }

    public void paintBackground() {

        Graphics BufferGraphics = BufferImg.getGraphics();
        BufferGraphics.fillRect(0, 0, width, height);
        BufferGraphics.drawImage(background, 0, 0, Graphics.TOP | Graphics.LEFT);
        //toPlayBtn.paint(BufferGraphics);
        for (int i = 0; i < listBackObjs.size(); i++) {
            System.out.println(listBackObjs.elementAt(i).getClass());
            ((MaterialObject) listBackObjs.elementAt(i)).paint(BufferGraphics);
        }

    }

    public void StartTimer() {
        sticker = new Sticker();
        timer.schedule(sticker, 50, 50);
        isChanging = true;
    }

    public void PauseTimer() {
        sticker.cancel();
        isChanging = false;
    }

    class Sticker extends TimerTask {

        public void run() {
            TimerStick();
            repaint();

        }
    }
}
